package com.tuju.jetpackfirstdemo.ui.wordcloud;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

import com.tuju.jetpackfirstdemo.R;
import com.tuju.jetpackfirstdemo.widget.WordCloudView;

import java.util.Arrays;
import java.util.List;
import java.util.Random;

public class WordCloudActivity extends AppCompatActivity {

    private WordCloudView wordCloudView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_word_cloud);
        initView();
        initData();
    }

    private void initView() {
        wordCloudView = findViewById(R.id.wcv);
        wordCloudView.setListener(new WordCloudView.WordCloudOnclick() {
            @Override
            public void onClickWord(String word) {
                Toast.makeText(WordCloudActivity.this, "点击了：" + word, Toast.LENGTH_SHORT).show();
            }
        });
    }

    private int index = 0;

    private void initData() {
        final List<WordCloudBean> keyWords = Arrays.asList(
                new WordCloudBean("风细柳斜斜", 24),
                new WordCloudBean("半壕春水一城花", 20),
                new WordCloudBean("烟雨暗千家", 18),
                new WordCloudBean("新火", 16),
                new WordCloudBean("酒醒", 22),
                new WordCloudBean("寒食节", 22),
                new WordCloudBean("今山东诸城", 20),
                new WordCloudBean("望", 8),
                new WordCloudBean("文学家", 22),
                new WordCloudBean("唐宋八大家", 18),
                new WordCloudBean("全集", 10),
                new WordCloudBean("触动乡思", 12),
                new WordCloudBean("字子瞻", 5),
                new WordCloudBean("四川人", 6),
                new WordCloudBean("书画", 9),
                new WordCloudBean("春色烟雨", 13),
                new WordCloudBean("公元1076年", 12)
        );

        final Handler handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                WordCloudBean bean = keyWords.get(index);
                wordCloudView.addTextView(bean.getWord(), bean.getWeight());
                index++;
                if (index < keyWords.size()) {
                    sendEmptyMessageDelayed(0, 100);
                }
            }
        };
        handler.sendEmptyMessage(0);
    }

    public static class WordCloudBean {
        private String word;
        private int weight;

        public WordCloudBean(String word, int weight) {
            this.word = word;
            this.weight = weight;
        }

        public String getWord() {
            return word;
        }

        public void setWord(String word) {
            this.word = word;
        }

        public int getWeight() {
            return weight;
        }

        public void setWeight(int weight) {
            this.weight = weight;
        }
    }

}
